package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.domain.LitemallAccount;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserExample;
import org.linlinjava.litemall.db.domain.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.github.pagehelper.PageHelper;

@Service
public class LitemallUserService {

  @Resource
  private LitemallUserMapper userMapper;

  @Resource
  private LitemallAccountService accountService;

  public LitemallUser findById(Integer userId) {
    return userMapper.selectByPrimaryKey(userId);
  }

  public UserVo findUserVoById(Integer userId) {
    LitemallUser user = findById(userId);
    UserVo userVo = new UserVo();
    userVo.setNickname(user.getNickname());
    userVo.setAvatar(user.getAvatar());
    return userVo;
  }

  public LitemallUser queryByOid(String openId) {
    LitemallUserExample example = new LitemallUserExample();
    example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
    return userMapper.selectOneByExample(example);
  }

  public void add(LitemallUser user) {
    LitemallAccount account = accountService.add(user.getNickname());
    user.setAddTime(LocalDateTime.now());
    user.setUpdateTime(LocalDateTime.now());
    user.setAccountId(account.getId());
    userMapper.insertSelective(user);
  }

  public int updateById(LitemallUser user) {
    user.setUpdateTime(LocalDateTime.now());
    return userMapper.updateByPrimaryKeySelective(user);
  }

  public List<LitemallUser> querySelective(String username, String mobile, Integer page,
      Integer size, String sort, String order) {
    LitemallUserExample example = new LitemallUserExample();
    LitemallUserExample.Criteria criteria = example.createCriteria();

    if (!StringUtils.isEmpty(username)) {
      criteria.andUsernameLike("%" + username + "%");
    }
    if (!StringUtils.isEmpty(mobile)) {
      criteria.andMobileEqualTo(mobile);
    }
    criteria.andDeletedEqualTo(false);

    if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
      example.setOrderByClause(sort + " " + order);
    }

    PageHelper.startPage(page, size);
    return userMapper.selectByExample(example);
  }

  public int count() {
    LitemallUserExample example = new LitemallUserExample();
    example.or().andDeletedEqualTo(false);

    return (int) userMapper.countByExample(example);
  }

  public List<LitemallUser> queryByUsername(String username) {
    LitemallUserExample example = new LitemallUserExample();
    example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
    return userMapper.selectByExample(example);
  }

  public boolean checkByUsername(String username) {
    LitemallUserExample example = new LitemallUserExample();
    example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
    return userMapper.countByExample(example) != 0;
  }

  public List<LitemallUser> queryByMobile(String mobile) {
    LitemallUserExample example = new LitemallUserExample();
    example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
    return userMapper.selectByExample(example);
  }

  public List<LitemallUser> queryByOpenid(String openid) {
    LitemallUserExample example = new LitemallUserExample();
    example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
    return userMapper.selectByExample(example);
  }

  public void deleteById(Integer id) {
    userMapper.logicalDeleteByPrimaryKey(id);
  }
}
